package com.xdap.init.controller;

import com.definesys.mpaas.common.http.Response;
import com.xdap.init.service.InitLastingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @auth lishushu
 * @2023/4/7 11:26
 * @desc {根据注释和字段名称生成对象}---初始化持久层的数据
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/df/init/lasting")
@Slf4j
public class InitLastingController {

    @Autowired
    private InitLastingService initLastingService;

    /**
     * @auth lishushu
     * @2023/4/7 14:32
     * @desc {生成po对象以及dao层对象}--根据formId创建，如果没有formId，则创建当前能访问到的所有页面
     * { 传入地址，formId，包名生成持久成的数据：持久层包括dao、po和vo}
     **/
    @PostMapping("/target")
    public Response target(@RequestParam("filePath") String filePath, @RequestParam("formId") String formId,
                           @RequestParam("packageName") String packageName) {
        Response res = new Response();
        try {
            packageName = packageName.toLowerCase();
            String result = initLastingService.initLasting(filePath, formId, packageName);
            res.setData(result);
        } catch (Exception e) {
            log.error("初始化持久层报错:{}", e);
            res.setCode(Response.CODE_ERR);
            res.setMessage(e.getMessage());
        }
        return res;
    }

    /**
     * @auth lishushu
     * @2023/4/21 11:06
     * @desc {只单独生成一个表名称的持久层数据}-根据表名称创建，切只根据当前表名称创建
     **/
    @PostMapping("/target/tableName")
    public Response targetTableName(@RequestParam("filePath") String filePath, @RequestParam("tableName") String tableName,
                                    @RequestParam("packageName") String packageName) {
        Response res = new Response();
        try {
            packageName = packageName.toLowerCase();
            String result = initLastingService.initLastingByTableName(filePath, tableName, packageName);
            res.setData(result);
        } catch (Exception e) {
            log.error("初始化持久层报错:{}", e);
            res.setCode(Response.CODE_ERR);
            res.setMessage(e.getMessage());
        }
        return res;
    }

    @PostMapping("/target/excelImport")
    public Response targetExcel(@RequestParam("filePath") String filePath) {
        Response res = new Response();
        try {
            String Result = initLastingService.targetExcel(filePath);
            res.setData(Result);
        }catch (Exception e){
            log.error("Excel导入失败：{}",e);
            res.setCode(Response.CODE_ERR);
            res.setMessage(e.getMessage());
        }
        return res;

    }

}
